package com.imagefake.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imagefake.entity.SysRoleUser;
import com.imagefake.entity.SysUser;
import com.imagefake.mapper.SysUserMapper;
import com.imagefake.service.SysRoleUserService;
import com.imagefake.service.SysUserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;

/**
 * (SysUser)表服务实现类
 *
 * @author makejava
 * @since 2024-03-05 20:06:19
 */
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    //密码加密器
    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysRoleUserService sysRoleUserService;

    @Override
    @Transactional
    public boolean save(SysUser sysUser) {
        //对存入数据库的密码进行加密
        sysUser.setPassword(passwordEncoder.encode(sysUser.getPassword()));
        sysUserMapper.insert(sysUser);
        Integer userId = sysUserMapper.getByUserName(sysUser.getUsername()).getUserId();
        sysRoleUserService.save(new SysRoleUser(userId,2));
        return true;
    }

    @Override
    public SysUser getByUserName(String username) {
        return sysUserMapper.getByUserName(username);
    }
}

